package com.kefu.call.http;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class CtiClient {
    @Value("${kefu.call.getCurl}")
    private String getCurl;

    public void reloadFsConf(String header) {
        ThreadUtil.execute(() -> {
            StrUtil.split(getCurl,",").forEach(item->{
                String url = item + "/reload/fs-conf";
                Map<String, Object> map = new HashMap<>();
                if(StrUtil.isNotBlank(header)){
                    map.put("header", header);
                }
                String result = HttpUtil.get(url, map);
                log.info("reloadFsConf:{},{},{}", url, map, result);
            });
        });
    }

    public void reloadFsMod(String mod) {
        ThreadUtil.execute(() -> {
            StrUtil.split(getCurl,",").forEach(item->{
                String url = item + "/reload/fs-mod";
                Map<String, Object> map = new HashMap<>();
                map.put("mod", mod);
                String result = HttpUtil.get(url, map);
                log.info("reloadFsMod:{},{},{}", url, map, result);
            });
        });
    }

    /**
     * 重新扫描 Sofia
     * @param gw 网关参数
     */
    public void rescanSofia(String gw) {
        // 使用线程工具类执行一个新的线程
        ThreadUtil.execute(() -> {
            // 将 getCurl 字符串按照逗号分割，然后对每个元素执行以下操作
            StrUtil.split(getCurl, ",").forEach(item -> {
                // 构造 URL，将 item 和 "/reload/rescan-sofia" 拼接
                String url = item + "/reload/rescan-sofia";
                Map<String, Object> map = new HashMap<>();
                map.put("gateway", gw);
                // 发送 GET 请求，并获取结果
                String result = HttpUtil.get(url, map);
                log.info("rescanSofia:{},{},{}", url, map, result);
            });
        });
    }

}
